package com.bbs.crawl;

import java.util.Map;
import java.util.Set;

import org.apache.log4j.Logger;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.bbs.bean.ReplyBBSOfBitAuto;
import com.bbs.dao.ReplyDaoOfBitAuto;
import com.car.util.DocUtil;
import com.car.util.Util;

public class ReplyCarwlBitAuto implements Runnable {
	private static Logger logger = Logger.getLogger(ReplyCarwlBitAuto.class.getName());
	private ReplyDaoOfBitAuto replyDaoOfBitAuto;
	private int hf_yhid;

	public ReplyCarwlBitAuto(ReplyDaoOfBitAuto replyDaoOfBitAuto, int hf_yhid) {
		super();
		this.replyDaoOfBitAuto = replyDaoOfBitAuto;
		this.hf_yhid = hf_yhid;
	}

	@Override
	public void run() {
		this.startCrawl(this.hf_yhid);
	}

	// 得到用户回复的所有页面数目
	public int getPages(int hf_yhid) {
		int page = 1;
		String url = "http://baa.bitauto.com/iyiche/ireplytopic-" + hf_yhid + "-1.html";
		Document doc = DocUtil.getDoc(url);
		if (doc == null) {
			System.out.println("DOC NULL" + "\t" + url);
			return 0;
		}
		Elements modify = doc.select("div.line_box");
		if (modify.text().length() == 0 || modify.text() == null) {
			page = 0;
			return page;
		}
		try {
			Elements pages = doc.select("div.the_pages a");
			int size = pages.size();
			Element pageSingle = pages.get(size - 2);
			page = Integer.parseInt(pageSingle.text());
		} catch (Exception e) {
		}
		return page;
	}

	// 爬虫入口 ，传入的是用户ID
	public void startCrawl(int hf_yhid) {
		Map<String, Integer> map = replyDaoOfBitAuto.correspond();
		Set<String> key = map.keySet();
		int page = getPages(hf_yhid);
		if (page == 0) {
			return;
		}
		logger.info(hf_yhid + "开始");
		int j = 0;
		for (int i = 1; i < page + 1; i++) {
			String url = "http://baa.bitauto.com/iyiche/ireplytopic-" + hf_yhid + "-" + i + ".html";
			Document doc = DocUtil.getDoc(url);
			if (doc == null) {
				System.out.println("DOC NULL" + "\t" + url);
				return;
			}
			Elements conList = doc.select("div.line_box div.postslist_xh");
			for (Element e : conList) {
				ReplyBBSOfBitAuto replyBBSOfBitAuto = null;
				try {
					if (e.select("li").size() == 4) {
						Element replyUser = e.select("div.huitie_box li.huitie_title a").get(0);
						int hf_hfuid = Integer.parseInt(Util.getMatched(replyUser.attr("href"), "\\d{4,}").group(0));
						Element reply = e.select("div.huitie_box li.huitie_title a").get(1);
						String link = reply.attr("href");
						String urlSpell = link.split("/")[3];
						int hf_clubid = key.contains(urlSpell) ? map.get(urlSpell) : 0;
						Integer hf_bbsid = Integer.parseInt(Util.getMatched(link, "\\d{4,}").group(0));
						String hf_bt = Util.deleteChar(reply.text());
						String hf_nr = Util.deleteChar(e.select("li.huitie_text").get(1).text());
						String hf_time = e.select("li.huitie_time").text().split("回复于")[1].trim();
						replyBBSOfBitAuto = new ReplyBBSOfBitAuto(hf_yhid, hf_clubid, hf_bbsid, hf_bt, hf_nr, hf_hfuid,
								hf_time);
						replyDaoOfBitAuto.insertReply(replyBBSOfBitAuto);
						j++;
					} else if (e.select("li").size() == 3) {
						Element reply = e.select("li.huitie_title").get(0);
						Element replyUser = reply.select("a").get(0);
						int hf_hfuid = Integer.parseInt(Util.getMatched(replyUser.attr("href"), "\\d{1,}").group(0));
						Element replyTopic = reply.select("a").get(1);
						String link = replyTopic.attr("href");
						String urlSpell = link.split("/")[3];
						int hf_clubid = key.contains(urlSpell) ? map.get(urlSpell) : 0;
						Integer hf_bbsid = Integer.parseInt(Util.getMatched(link, "(.*-)(\\d{1,})(\\.html)").group(2));
						String hf_bt = Util.deleteChar(replyTopic.text());
						Element content = e.select("li.huitie_text").get(0);
						String hf_nr = Util.deleteChar(content.text());
						Element time = e.select("li.huitie_time").get(0);
						String hf_time = time.text().split("回复于")[1].trim();
						replyBBSOfBitAuto = new ReplyBBSOfBitAuto(hf_yhid, hf_clubid, hf_bbsid, hf_bt, hf_nr, hf_hfuid,
								hf_time);
						replyDaoOfBitAuto.insertReply(replyBBSOfBitAuto);
						j++;
					}
				} catch (Exception ee) {
					System.out.println(url);
					ee.printStackTrace();
					continue;
				}
			}
		}
		logger.info(hf_yhid + "结束" + "\t" + j);
	}
}
